package main

import (
	"fmt"
	"time"
)

//错误示例
//expect print a,b,c
//but print c,c,c
func noBind() {
	values := []string{"a", "b", "c"}
	for _, value := range values {
		go func() {
			fmt.Println("noBind:", value)
		}()
	}
}

//正确示例
//expect print a,b,c
//but print a,b,c
func withBind() {
	values := []string{"a", "b", "c"}
	for _, value := range values {
		go func(v string) {
			fmt.Println("withBind:", v)
		}(value)
	}
}

//正确示例
//expect print a,b,c
//but print a,b,c
func withBind2() {
	values := []string{"a", "b", "c"}
	for _, value := range values {
		v := value
		go func() {
			fmt.Println("withBind2:", v)
		}()
	}
}

func main() {
	noBind()
	withBind()
	withBind2()
	time.Sleep(time.Second)
}
